package com.atguigu.gulimall.order.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ming
 * @create 2022/5/9
 * @description:
 */
@Configuration
public class MyFeignConfig {
    @Bean
    public RequestInterceptor requestInterceptor(){
         return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                //获取请求上下文
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

                if(attributes!=null){
                    //老请求
                    HttpServletRequest request = attributes.getRequest();
                    //同步请求头数据 cookie
                    String cookie = request.getHeader("Cookie");
                    //给新的请求同步了cookie
                    requestTemplate.header("Cookie", cookie);
                }


            }
        }  ;
        
    }
}
